/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package axil.definition;

import axil.engine.Repository;
import axil.framework.validation.ValidationContext;
import axil.framework.xml.InputXML;
import axil.stdlib.collection.type.Range;

import java.util.ArrayList;
import java.util.List;


/**
 * This is the object representing the XML definition of a module.
 */
public class Module extends Definition {
	private List<String> dependencies;
	private List<String> typeNames;
	private List<String> operators;
	private List<ConstantDefinition> constants;
	private List<String> functions;

	private transient List<FunctionDefinition> funcs;
	private transient List<OperatorDefinition> ops;
	private transient List<TypeDefinition> types;


	public Module(InputXML xml) {
		super(xml);
		dependencies = xml.strings("dependencies", "depends-on", Range.any);
		typeNames = xml.strings("types", "type", Range.any);
		operators = xml.strings("operators", "operator", Range.any);
		constants = xml.objects(ConstantDefinition.class, "constants", Range.any);
		functions = xml.strings("functions", "function", Range.any);
	}


	public List<String> dependencies() {
		return dependencies;
	}


	public List<FunctionDefinition> functions() {
		return funcs;
	}


	public List<ConstantDefinition> constants() {
		return constants;
	}


	public List<OperatorDefinition> operators() {
		return ops;
	}


	public List<TypeDefinition> types() {
		return types;
	}


	/**
	 * Validate this object in the given context. Any problems detected with the
	 * correctness or completeness of this object are placed into the context.
	 * This method descends into its owned members, validating them as well.
	 */
	public void validate(ValidationContext context) {
		super.validate(context);
		Repository r = Repository.instance();
		String mod = this.identity();

		types = new ArrayList<TypeDefinition>(typeNames.size());
		for (String type : typeNames) {
			types.add(r.type(mod, type));
		}
		context.validate("types", types, Range.any);

		context.validate("constants", constants, Range.any);

		ops = new ArrayList<OperatorDefinition>(operators.size());
		for (String op : operators) {
			ops.add(r.operator(mod, op));
		}
		context.validate("operators", ops, Range.any);

		this.funcs = new ArrayList<FunctionDefinition>(functions.size());
		for (String func : functions) {
			FunctionDefinition f = r.function(mod, func);
			f.link(this);
			funcs.add(f);
		}
		context.validate("functions", funcs, Range.any);

		for (FunctionDefinition f : funcs) {
			f.compile(this, context);
		}
	}
}
